package com.pzb.web.controller.goods;

import java.util.List;

import com.pzb.common.utils.StringUtils;
import com.pzb.goods.domain.GdFreightTemplateData;
import com.pzb.wechat.service.IWechatAppletService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pzb.common.annotation.Log;
import com.pzb.common.core.controller.BaseController;
import com.pzb.common.core.domain.AjaxResult;
import com.pzb.common.enums.BusinessType;
import com.pzb.goods.domain.GdFreightTemplate;
import com.pzb.goods.service.IGdFreightTemplateService;
import com.pzb.common.core.page.TableDataInfo;

/**
 * 运费模板Controller
 * 
 * @author Mr_Wu
 * @date 2021-12-06
 */
@RestController
@RequestMapping("/goods/template")
public class GdFreightTemplateController extends BaseController
{
    @Autowired
    private IGdFreightTemplateService gdFreightTemplateService;

    @Autowired
    private IWechatAppletService wechatAppletService;

    /**
     * 查询运费模板列表
     */
    @PreAuthorize("@ss.hasPermi('goods:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(GdFreightTemplate gdFreightTemplate)
    {
        startPage();
        gdFreightTemplate.setExtAppid(wechatAppletService.getExtAppid());
        List<GdFreightTemplate> list = gdFreightTemplateService.selectGdFreightTemplateList(gdFreightTemplate);
        return getDataTable(list);
    }

    /**
     * 查询运费模板列表
     */
    @PreAuthorize("@ss.hasPermi('goods:template:list')")
    @GetMapping("/templateList")
    public AjaxResult templateList(GdFreightTemplate gdFreightTemplate)
    {
        gdFreightTemplate.setExtAppid(wechatAppletService.getExtAppid());
        List<GdFreightTemplate> list = gdFreightTemplateService.selectGdFreightTemplateList(gdFreightTemplate);
        return AjaxResult.success(list);
    }

    /**
     * 获取运费模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('goods:template:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gdFreightTemplateService.selectGdFreightTemplateById(id));
    }

    /**
     * 新增运费模板
     */
    @PreAuthorize("@ss.hasPermi('goods:template:add')")
    @Log(title = "运费模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GdFreightTemplate gdFreightTemplate)
    {
        String extAppid = wechatAppletService.getExtAppid();
        if("1".equals(extAppid)){
            return AjaxResult.error("您还没有小程序!");
        }
        gdFreightTemplate.setExtAppid(extAppid);
        for (GdFreightTemplateData templateData : gdFreightTemplate.getTemplateDataList()) {
            if(StringUtils.isNull(templateData.getFirstValue()) || StringUtils.isNull(templateData.getFirstAmount())|| StringUtils.isNull(templateData.getOtherValue())|| StringUtils.isNull(templateData.getOtherAmount())){
                return  AjaxResult.error("非包邮地区信息填写不完善,请检查后提交！");
            }
            if("0".equals(templateData.getAppoint())){
                if(StringUtils.isNull(templateData.getAppointType()) || StringUtils.isNull(templateData.getAppointValue())){
                    return  AjaxResult.error("非包邮地区信息填写不完善,请检查后提交！");
                }
            }
        }

        return toAjax(gdFreightTemplateService.insertGdFreightTemplate(gdFreightTemplate));
    }

    /**
     * 修改运费模板
     */
    @PreAuthorize("@ss.hasPermi('goods:template:edit')")
    @Log(title = "运费模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GdFreightTemplate gdFreightTemplate)
    {
        return toAjax(gdFreightTemplateService.updateGdFreightTemplate(gdFreightTemplate));
    }

    /**
     * 删除运费模板
     */
    @PreAuthorize("@ss.hasPermi('goods:template:remove')")
    @Log(title = "运费模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        if (gdFreightTemplateService.checkTemplateExistGoods(id))
        {
            return AjaxResult.error("模板存在关联商品,不允许删除");
        }
        return toAjax(gdFreightTemplateService.deleteGdFreightTemplateById(id));
    }
}
